
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_module/config/ServiceUlr.dart';
import 'package:flutter_module/service/serviee_method.dart';
import 'package:html/parser.dart' show parse;

class QueryModel with ChangeNotifier {
  //SPUtils sp=SPUtils();
  String userId;
  String companyId;
  int num=1;
  int page=1;
  bool query=false;
  Map map;
  List<List> data=[];
  List itemList=[];
  EasyRefreshController controller = EasyRefreshController();
//  Future getSP() async{
//   await sp.getUserInfo("userId").then((value)=>userId=value??"8a2a2a295c1ec767015c2ddd9d1e0001");
//   await sp.getUserInfo("companyId").then((value)=>companyId=value??"402880185889ef6e015889f8c51e0000");
//  }
  Future onLoad() async {
    if(num>1){
      await getHttpData(data: map).then((value){
        controller.finishLoad(success: true,noMore:page>=num);
      });
    }else{
      data.clear();
      await getHttpData(data: map).then((value){
        controller.finishLoad(success: true,noMore:page>=num);
      });
    }

  }
  Future getHttpData({Map data}) async{

    Map<String,String> map=Map();
    map["buildPlan"]="";
    map["department"]="";
    map["eventReason"]="";
    map["eventType"]="";
    map["jssj"]="";
    map["kssj"]="";
    map["organization"]="";
    map["pipeRadius"]="";
    map["projectName"]="";
    map["projectStatus"]="";
    map["pageIndex"]=page.toString();
    //map["pageSize"]="1";
    map["companyId"]="402880185889ef6e015889f8c51e0000";
    map["userId"]="8a2a2a295c1ec767015c2ddd9d1e0001";
    if(data!=null){
      if(query){
      }else{
        query=!query;
        page=1;
        this.data.clear();
      }

      data["pageIndex"]=page.toString();
      data["companyId"]="402880185889ef6e015889f8c51e0000";
      data["userId"]="8a2a2a295c1ec767015c2ddd9d1e0001";
    }
    await getHttpPost(servicePath["query"], data??map).then((value){
      this.page++;
      getListData(value);
      notifyListeners();
    });

  }


  void getListData(value) {
     var document = parse(value);
        var body= document.getElementsByTagName("tr");
     var span= document.getElementsByTagName("span");
     for(int i=0;i<span.length;i++){
       String name= span[2].outerHtml.substring(25,span[2].outerHtml.length-7).trim();
       if(i==2){
      num=int.parse(name);
       }

     }
      for(int i=1;i<body.length;i++){
         var list=body[i].getElementsByTagName("td");
         List item=[];
      for(int j=1;j<list.length;j++){
       String name= list[j].outerHtml.substring(4,list[j].outerHtml.length-5).trim();
       item.add(name);
      }
     data.add(item);
      }

  }

}